﻿package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.badibadi.infos.CreateNewClubChoseTypeInfos;
import com.badibadi.infos.CreateNewClubModel;
import com.badibadi.infos.ListsModel;
import com.badibadi.infos.MasterModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Convert;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

import android.os.Bundle;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CreationNewClubActivity extends ExpertApplyForChoseTypeActivity {

	private TextView title;
	private String KEY = "CreationNewClubActivity";

	private String uid;
	private CreateNewClubModel createNewClubModel;
	private ListsModel listsModel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		createNewClubModel = new CreateNewClubModel();
		uid = Utils.getUid(CreationNewClubActivity.this);
		init();

	}

	/** 复用选择达人需要重写四处，这是第一处 重写文字的数量 */
	@Override
	protected void initItemNum(int num) {
		// TODO Auto-generated method stub
		super.initItemNum(num);
	}

	/**
	 * 重写的第二处 ，从网络上获取数据
	 */
	@Override
	protected void initInterNet() {
		// TODO Auto-generated method stub
		Utils.showPrgress(CreationNewClubActivity.this);
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				listsModel = new ListsModel();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", "julebu");
				map.put("languageType", com.badibadi.mytools.Dialog
						.getSystemLanguageType(CreationNewClubActivity.this));
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "type/listType");
				//    
				if (content != null) {
					//    
					Results results = Utils.checkResult_NNN(
							CreationNewClubActivity.this, content);
					if (results != null && results.getRetmsg() != null) {
//						   
						try {
							listsModel = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), ListsModel.class);
							if (listsModel.getLists() != null
									&& !listsModel.getLists().isEmpty()) {
								for (int i = 0; i < listsModel.getLists()
										.size(); i++) {
									MasterModel masterModel = new MasterModel();
									masterModel.setId(listsModel.getLists()
											.get(i).getId());
									masterModel.setName(listsModel.getLists()
											.get(i).getName());
									masterListView.add(masterModel);
								}
							}
							for (int i = 0; i < masterListView.size(); i++) {
								list.add(masterListView.get(i).getName());
							}
							ItemNum = listsModel.getLists().size();
							mHandler.sendEmptyMessage(0);

						} catch (Exception e) {
							// TODO Auto-generated catch block
//							   
							   
						}
					}
				} else {
					Utils.ExitPrgress(CreationNewClubActivity.this);
					mHandler.sendEmptyMessage(1);
				}
			}
		}).start();
	}

	/**
	 * 返回图片名 返回图片的名称（把从网络获取的返回过去就行了） 重写第三处
	 */
	@Override
	public String[] getTuPianMing(String[] tupianming) {
		// TODO Auto-generated method stub
		// 在这里，不满九张的最好加上,不知道为什么，不加的话图片会拉长
		if (list.size() % 9 != 0) {
			for (int i = 0; i < list.size() % 9; i++) {
				list.add("");
			}
		}
		return Convert.ListTransitionString(list);
	}

	/**
	 * 重写第四处 （把网络上获取的图片返回）
	 */
	@Override
	public String[] getBeiJingTu(String[] beijingtu) {
		// TODO Auto-generated method stub
		return super.getBeiJingTu(beijingtu);
	}

	private void init() {
		title = (TextView) this
				.findViewById(R.id.expert_appley_for_chose_type_title);
		title.setText(getResources().getString(R.string.CreateClub));
		TextView chose = (TextView) this
				.findViewById(R.id.expert_appley_for_chose_club_type);
		chose.setText(getResources().getString(R.string.PleaseSelectAClubType));

		Button btn = (Button) this.findViewById(R.id.next_step);
		btn.setText(getResources().getString(R.string.CreateClubFirststep));
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (GetData() != "") {
//					   
					Intent intent = new Intent(CreationNewClubActivity.this,
							CreationNewClub2Activity.class);

					createNewClubModel.setTid("" + GetDataId());
					createNewClubModel.setUid(uid);
					intent.putExtra("createNewClubModel", createNewClubModel);
					startActivity(intent);
					overridePendingTransition(R.anim.slide_left,
							R.anim.slide_right);
					CreationNewClubActivity.this.finish();
				} else if (GetData() == "") {
					Toast.makeText(
							CreationNewClubActivity.this,
							""
									+ getResources().getString(
											R.string.PleaseSelectAClubType),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

}
